function getXMLHttpRequest()
{
    var xhr = null;

    if (window.XMLHttpRequest)
        xhr = new XMLHttpRequest();
    else
    if (window.ActiveXObject)
    {
        try {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    else
    {
        alert("Your web browser do not support XmlHttpRequest.");
        xhr = false;
    }
    return xhr;
}

function l_steps() {
    var xhr = getXMLHttpRequest();
    xhr.onreadystatechange = function()
    {
        if (xhr.readyState === 4)
            if (xhr.status === 200) {
                {
                    var reponse1 = xhr.responseXML.getElementsByTagName("ordre");
                    var reponse2 = xhr.responseXML.getElementsByTagName("step");
                    var reponse3 = xhr.responseXML.getElementsByTagName("stat");

                    var text = "<table>";
                    text += "<tr>";
                    text += "<th><p class=\"text11boldwhite\">";
                    text += "Order";
                    text += "</p></th>";
                    text += "<th><p class=\"text11boldwhite\">";
                    text += "Step";
                    text += "</p></th>";
                    text += "<th><p class=\"text11boldwhite\">";
                    text += "State";
                    text += "</p></th>";
                    text += "</tr>";
                    for (var i = 0; i < reponse1.length; i++) {
                        text += "<tr>";
                        text += "<td><p>" + reponse1[i].firstChild.nodeValue + "</p></td>";
                        text += "<td><p>" + reponse2[i].firstChild.nodeValue + "</p></td>";
                        text += "<td><p>" + reponse3[i].firstChild.nodeValue + "</p></td>";
                        text += "</tr>";
                    }
                    var elt = document.getElementById("tablestep");
                    elt.innerHTML = text + "</table>";
                }
            }
    }

//    var parms = "dapv=" + encodeURIComponent(document.getElementById("dapvselected").value);
//    xhr.open("GET", "../ServletValidateDap?" + parms, true);
////    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//    xhr.send();
    var lstdap = document.getElementById("dapvselected");
    var dapselected = lstdap.options[lstdap.selectedIndex].value;

    var parms = "dapv=" + dapselected;
//    alert(parms);
    xhr.open("GET", "ServletValidateDap?" + parms, true);
//    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send();

}

function validate() {
    var xhr = getXMLHttpRequest();
    var lstdap = document.getElementById("dapvselected");
    var dapselected = lstdap.options[lstdap.selectedIndex].value;

    var parms = "dapv=" + dapselected;
    xhr.open("GET", "ServletDapvalidated?" + parms, true);
    xhr.send();
}

function validateDAP() {
    var xhr = getXMLHttpRequest();
    xhr.onreadystatechange = function()
    {
        if (xhr.readyState === 4)
            if (xhr.status === 200) {
                {
                    var reponse = xhr.responseXML.getElementsByTagName("stat");
                    var nb = 0;
                    var boolean = true;
                    while (nb < reponse.length && boolean === true) {
                        if (reponse[nb].firstChild.nodeValue !== "Validated") {
                            boolean = false;
                            alert("Not all steps are validated");
                            return false;
                        }
                        nb++;
                    }
                    validate();
                    alert("Validation succeed!");
                    return true;
                }
            }
    }
    var lstdap = document.getElementById("dapvselected");
    var dapselected = lstdap.options[lstdap.selectedIndex].value;
    var parms = "dapv=" + dapselected;
    xhr.open("GET", "ServletValidateDAPAction?" + parms, true);
    xhr.send();
}

function Dapvalidated() {
    var xhr = getXMLHttpRequest();
    xhr.onreadystatechange = function()
    {

        if (xhr.readyState === 4)
//            alert("dfdqf");
            if (xhr.status === 200) {
                {
//                alert("dfdqf");
                    var reponse = xhr.responseXML.getElementsByTagName("dap");
//                alert("dfdqf");
                    var text = "<table>";
                    text += "<tr>";
                    text += "<th><p class=\"text11boldwhite\">";
                    text += "Validated processes";
                    text += "</th></p>";
                    text += "</tr>";
                    for (var i = 0; i < reponse.length; i++) {
                        text += "<tr>";
                        text += "<td><p>" + reponse[i].firstChild.nodeValue + "</p></td>";
                        text += "</tr>";
                    }
                    var elt = document.getElementById("DapValidate");
                    elt.innerHTML = text + "</table>";
                }
            }
    }
//    var lstdap = document.getElementById("dapvselected");
//    var dapselected = lstdap.options[lstdap.selectedIndex].value;
//    var parms = "dapv=" + dapselected;
    xhr.open("GET", "ServletfindDapvalidated?", true);
    xhr.send();
}

function pourcentage() {
    var xhr = getXMLHttpRequest();
    xhr.onreadystatechange = function()
    {

        if (xhr.readyState === 4)
//            alert("dfdqf");
            if (xhr.status === 200) {
                {

                    var reponse1 = xhr.responseXML.getElementsByTagName("ordre");
                    var reponse2 = xhr.responseXML.getElementsByTagName("step");
                    var reponse3 = xhr.responseXML.getElementsByTagName("nbvalidate");
                    var reponse4 = xhr.responseXML.getElementsByTagName("percentagevalidate");

                    var text = "<table>";
                    text += "<tr>";
                    text += "<th><p class=\"text11boldwhite\">";
                    text += "Steps / Order";
                    text += "</th></p>";
                    text += "<th><p class=\"text11boldwhite\">";
                    text += "Validations (% of total)";
                    text += "</th></p>";
//                    text += "<td>";
//                    text += "Validations(costumer) (% of total)";
//                    text += "</td>";
                    text += "</tr>";
                    for (var i = 0; i < reponse1.length; i++) {
                        text += "<tr>";
                        text += "<td><p>" + reponse2[i].firstChild.nodeValue + " / " + reponse1[i].firstChild.nodeValue + "</p></td>";
                        text += "<td><p><b>" + reponse3[i].firstChild.nodeValue + "</b> ("+reponse4[i].firstChild.nodeValue+"%)"+"</p></td>";
//                        text += "<td bgcolor=\"#A9E2F3\">" + reponse3[i].firstChild.nodeValue + "("+reponse4[i].firstChild.nodeValue+"% )"+"</td>";
                        text += "</tr>";
                    }
                    var elt = document.getElementById("tableindicator");
                    elt.innerHTML = text + "</table>";
                }
            }
    }
    var lstdap = document.getElementById("DapStructure");

    var DapStructure = lstdap.options[lstdap.selectedIndex].value;
//    alert(DapStructure+"------------");
    var parms = "iddap=" + DapStructure;
    xhr.open("GET", "Servletprocessownerindicator?" + parms, true);
    xhr.send();
}

function pourcentagecostumer() {
    var xhr = getXMLHttpRequest();
    xhr.onreadystatechange = function()
    {
        
        if (xhr.readyState === 4)

            if (xhr.status === 200) {
                {
                    
                    var reponse1 = xhr.responseXML.getElementsByTagName("ordre");
                    var reponse2 = xhr.responseXML.getElementsByTagName("step");
                    var reponse3 = xhr.responseXML.getElementsByTagName("nbvalidate");
                    var reponse4 = xhr.responseXML.getElementsByTagName("percentagevalidate");
                    var reponse5 = xhr.responseXML.getElementsByTagName("nbvalidatecostumer");
                    var reponse6 = xhr.responseXML.getElementsByTagName("percentagevalidatecostumer");

                    var text = "<table>";
                    text += "<tr>";
                    text += "<th><p class=\"text11boldwhite\">";
                    text += "Steps / Order";
                    text += "</th></p>";
                    text += "<th><p class=\"text11boldwhite\">";
                    text += "Validations (% of total)";
                    text += "</th></p>";
                    text += "<th><p class=\"text11boldwhite\">";
                    text += "Validations(costumer) (% of total)";
                    text += "</th></p>";
                    text += "</tr>";
                    for (var i = 0; i < reponse1.length; i++) {
                        text += "<tr>";
                        text += "<td><p>" + reponse2[i].firstChild.nodeValue + " / " + reponse1[i].firstChild.nodeValue + "</p></td>";
                        text += "<td><p><b>" + reponse3[i].firstChild.nodeValue + "</b> ("+reponse4[i].firstChild.nodeValue+"%)"+ "</p></td>";
                        text += "<td bgcolor=\"#A9E2F3\"><p>" + reponse5[i].firstChild.nodeValue + "("+reponse6[i].firstChild.nodeValue+"% )"+ "</p></td>";
                        text += "</tr>";
                    }
                    var elt = document.getElementById("tableindicator");
                    elt.innerHTML = text + "</table>";
                }
            }
    }
    
    var lstdap = document.getElementById("DapStructure");
    var lstcos = document.getElementById("customer");
    
    var DapStructure = lstdap.options[lstdap.selectedIndex].value;
    var costumer = lstcos.options[lstcos.selectedIndex].value;
    var parms = "iddap=" + DapStructure +"&idcos=" + costumer;
//    var parm="idcos="+costumer;
//    alert("bonsoir"+parms);
//    alert("bonsoir"+parm);
    xhr.open("GET", "Servletcustomerselecedindicator?" + parms, true);
    xhr.send();
}

function getcostumer() {
    var xhr = getXMLHttpRequest();
    xhr.onreadystatechange = function()
    {

        if (xhr.readyState === 4)
//            alert("dfdqf");
            if (xhr.status === 200) {
                {
                    var reponse = xhr.responseXML.getElementsByTagName("costumer");
//                    alert("hello");
                    var text = "<select id=\"customer\" onclick=\"pourcentagecostumer()\">";
//                    text += "<option>"+" All "+"</option>";
                    for (var i = 0; i < reponse.length; i++) {
                        text += "<option value="+reponse[i].firstChild.nodeValue+">"+reponse[i].firstChild.nodeValue+"</option>";
                    }
                    var elt = document.getElementById("zoneselectclient");
                    elt.innerHTML = text + "</select>";
                }
            }
    }
    var lstdap = document.getElementById("DapStructure");
    
    var DapStructure = lstdap.options[lstdap.selectedIndex].value;
    var parms = "iddap=" + DapStructure;
//    alert("hello"+parms);
    xhr.open("GET", "Servletfindcostumerofstructure?" + parms, true);
    xhr.send();
}